﻿using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.Context {

    /// <summary>
    /// List all variables in the context
    /// </summary>
    [Function("ls", "Lists all variables in the context", "a=1;b=2;ls();", "a=1;\nb=2;\n")]
    public class LsNode : ContextFuncNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public LsNode()
            : base("ls") {
        }

        /// <summary>
        /// Evaluate the expression
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            if (Parameters.Count == 0
                ||
                Parameters[0].Value == "varaible") {

            } else if (Parameters[0].Value == "function") {
                StringBuilder buff = new StringBuilder();

                foreach (string funcName in base.Context.GetFunctionManager().Functions.Keys) {
                    buff.Append(funcName).Append(",");
                }

                return buff.ToString();
            } else {
                return "Unrecognisable parameter " + Parameters[0].Value;
            }

            return string.Empty;
        }
    }
}
